package com.justin.baidumap.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.utils.DistanceUtil;
import com.justin.baidumap.R;

import java.util.List;

/**
 * Created by developer on 16/1/20.
 */
public class PoiSearchAdapter extends BaseAdapter {

	private Context mContext;
	private List<PoiInfo> poiInfos;
	private LatLng locationLatLng;

	public PoiSearchAdapter(Context context, List<PoiInfo> poiInfos, LatLng locationLatLng) {
		this.mContext = context;
		this.poiInfos = poiInfos;
		this.locationLatLng = locationLatLng;
	}

	@Override
	public int getCount() {
		return poiInfos != null ? poiInfos.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		return poiInfos != null ? poiInfos.get(position) : null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.poisearch_item,null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		PoiInfo poiInfo = poiInfos.get(position);
		holder.poisearch_name.setText(poiInfo.name);
		holder.poisearch_address.setText(poiInfo.address);
		double distance = DistanceUtil.getDistance(locationLatLng, poiInfo.location);
		holder.poisearch_distance.setText(String.valueOf((int)(Math.round(distance)))+"米");
		return convertView;
	}

	class ViewHolder{
		private TextView poisearch_name;
		private TextView poisearch_address;
		private TextView poisearch_distance;

		public ViewHolder(View convertView) {
			poisearch_name = (TextView) convertView.findViewById(R.id.poisearch_name);
			poisearch_address = (TextView) convertView.findViewById(R.id.poisearch_address);
			poisearch_distance = (TextView) convertView.findViewById(R.id.poisearch_distance);
		}
	}
}
